from flask import jsonify, request
from flask_jwt_extended import jwt_required, get_jwt_identity, create_access_token
from .models import User
from .extensions import db


def init_main_routes(app):
    @app.route('/api/auth/verify', methods=['GET'])
    @jwt_required()
    def verify_token():
        try:
            # 获取当前用户的身份
            current_user_id = int(get_jwt_identity())
            user = User.query.get(current_user_id)

            if not user:
                return jsonify({
                    'success': False,
                    'message': '用户不存在'
                }), 401

            return jsonify({
                'success': True,
                'username': user.username,
                'message': '验证成功'
            })

        except Exception as e:
            return jsonify({
                'success': False,
                'message': f'验证失败: {str(e)}'
            }), 500

    @app.route('/api/auth/logout', methods=['POST'])
    @jwt_required()
    def logout():
        try:
            # 在实际应用中，这里可以添加token黑名单等逻辑
            return jsonify({
                'success': True,
                'message': '退出登录成功'
            })

        except Exception as e:
            return jsonify({
                'success': False,
                'message': f'退出登录失败: {str(e)}'
            }), 500